from scrapy.spider import Spider
import pymongo
import time
import json
import datetime

mongo_ip='db.stormjoy.com'
mongo_db='tradedb'

class DepthSpider(Spider):
    name = "depth"
    allowed_domains = ["okcoin.com","huobi.com"]
    start_urls = [
        "https://www.okcoin.com/api/depth.do?symbol=ltc_cny",
        "http://market.huobi.com/staticmarket/depth_ltc_json.js"
    ]
    def __init__(self, category=None, *args, **kwargs):
        super(DepthSpider, self).__init__(*args, **kwargs)
        self.t = int(time.time())
        self.date = datetime.datetime.fromtimestamp(self.t).strftime('%Y%m%d')
    def parse(self, response):
        res = json.loads(response.body)
        con = pymongo.Connection(mongo_ip, 27017)
        res['t'] = self.t 
        if response.url == self.start_urls[0]:
            res['type'] = "ok_ltc"
        else:
            res['type'] = "hb_ltc"
        collection = con[mongo_db]['depth'+self.date]
        collection.insert(res)
